<?php
/**
 * Created by PhpStorm.
 * User: kai.yang
 * Date: 2019-01-01
 * Time: 16:51
 */

namespace Utils;

use Model\LoginModel;
use Exception\ErrorPassowrdException;
use Exception\NoUserException;

class LoginUtils
{
    public static function checkLogin($loginName,$password){
        $model=M("users u");
        $map=array();
        $map['name']=$loginName;
        $result=$model->field("u.name,u.deploy_name,p.pass")->join("left join password p on  u.id=p.user_id")
            ->where($map)->order('u.Create_time desc')->limit(2)->select();
        if(empty($result)){
            $noUserException = new NoUserException("用户不存在");
            return $noUserException;
        }else if($result[0]["pass"]!=$password){
            return new ErrorPassowrdException("密码错误!");
        }else {
            return new LoginModel($loginName,$password,$result[0]['deploy_name']);
        }
    }
}